package network4;

import java.io.*;
import java.net.Socket;

//服务器端多线程处理程序
public class ServerThread implements Runnable {
    private Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            int count = 0;
            //给客户端传过来的文件重命名
            File file = new File("C:\\networkTest1\\Copy[" + count + "].txt");
            while ((file.exists())) {
                count++;
                file = new File("C:\\networkTest1\\Copy[" + count + "].txt");
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            //给出反馈
            BufferedWriter bwserver = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bwserver.write("yesyes");
            bwserver.newLine();
            bwserver.flush();
            s.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}
